package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
	"time"
	"zg4/online-retailers/api-gateway/request"
	"zg4/online-retailers/common/global"
	"zg4/online-retailers/common/proto/products"
	"zg4/online-retailers/pkg"
)

func AddProduct(c *gin.Context) {
	var req request.AddProductReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	// 单文件
	file, _ := c.FormFile("file")
	ext := filepath.Ext(file.Filename)
	filename := time.Now().Format("20060102150405") + ext
	dst := "./static/img/" + filename
	// 上传文件至指定的完整文件路径
	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	pkg.AliYunOss(dst, filename)

	_, err = global.ProductClient.AddProduct(c, &products.AddProductRequest{
		Title: req.Title,
		Cover: filename,
		Price: uint64(req.Price),
		Stock: uint64(req.Stock),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发布商品成功",
		"data": nil,
	})

}

func ProductList(c *gin.Context) {
	var req request.ProductListReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	list, err := global.ProductClient.ProductList(c, &products.ProductListRequest{
		Page: uint64(req.Page),
		Size: uint64(req.Size),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "商品列表展示成功",
		"data": list,
	})

}

func GroupBuy(c *gin.Context) {
	var req request.GroupBuyReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	v, err := global.ProductClient.GroupBuy(c, &products.GroupBuyRequest{
		UserId:    uint64(req.UserId),
		Username:  req.Username,
		Phone:     req.Phone,
		AddressId: uint64(req.AddressId),
		ProductId: uint64(req.ProductId),
		Num:       uint64(req.Num),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "拼团成功",
		"data": v,
	})
}

func GroupBuyInfo(c *gin.Context) {
	var req request.GroupBuyInfoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	info, err := global.ProductClient.GroupBuyInfo(c, &products.GroupBuyInfoRequest{
		GroupById: uint64(req.GroupBuyId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "拼团详情信息展示成功",
		"data": info,
	})
}
